package com.pactera.jep.service.sys.security.spring.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.pactera.jep.entity.BasePO;
import com.pactera.jep.service.sys.model.UserProfile;
import com.pactera.jep.sys.dto.MenuDto;
import com.pactera.jep.sys.model.Staff;
import com.pactera.jep.sys.model.User;

/**
 * 用户登录实体类 TODO:修改，将{@link UserProfile}的属性集成进来，这样在去掉Shiro之后可以获取到数据
 * 
 * @author ghost
 *
 */
public class UserEntity extends BasePO implements UserDetails {

	private static final long serialVersionUID = -5799195512594207526L;

	public UserEntity() {

	}

	public UserEntity(User user) {
		this.userId = user.getUserId();
		this.userName = user.getUserName();
		this.password = user.getPassword();
		this.passwordExpireDays = user.getPasswordExpireDays();
		this.passwordExpireTime = user.getPasswordExpireTime();
		this.accountExpireDays = user.getAccountExpireDays();
		this.failedCount = user.getFailedCount();
		this.loginIp = user.getLoginIp();
		this.loginTime = user.getLoginTime();
		this.maxFailedCount = user.getMaxFailedCount();
		this.accountNonExpired = user.getAccountNonExpired() >= 1;
		this.accountNonLocked = user.getAccountNonLocked() < 1;
		this.credentialNonExpired = user.getCredentialNonExpired() >= 1;
		this.enabled = user.getEnabled() >= 1;
		// BasePO相关的属性
		this.setTenantId(user.getTenantId());
		this.setCreateById(user.getCreateById());
		this.setCreateByName(user.getCreateByName());
		this.setCreateTime(user.getCreateTime());
		this.setModifiedById(user.getModifiedById());
		this.setModifiedByName(user.getModifiedByName());
		this.setModifiedTime(user.getModifiedTime());
		this.setDelflag(user.getDelflag());
		this.setDisplayOrder(user.getDisplayOrder());
		this.setVersion(user.getVersion());
	}

	/***************
	 * User类中的属性 *
	 **************/

	private String userId;

	private String userName;

	private String password;

	private Integer passwordExpireDays;

	private Date passwordExpireTime;

	private Integer accountExpireDays;

	private Date accountExpireTime;

	private Integer failedCount;

	private String loginIp;

	private Date loginTime;

	private Integer maxFailedCount;

	/**
	 * 用户登录相关token，用来判断用户的认证是否已经过期
	 */
	private String token;

	/**
	 * 账号是否可用，默认为true
	 */
	private boolean enabled = true;

	/**
	 * 账号是否没有过期，默认为true
	 */
	private boolean accountNonExpired = true;

	/**
	 * 账号是否没有锁定，默认为true
	 */
	private boolean accountNonLocked = true;

	private boolean credentialNonExpired = true;

	/**
	 * 用户信息
	 */
	private Staff staff;

	/**
	 * 菜单
	 */
	private List<MenuDto> menus;

	/**
	 * 用户角色
	 */
	private List<String> roles;


	/**
	 * 获取用户所有的权限
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// 当前暂时没有做url跟角色，所以不用对权限进行绑定，所有的url只需要登录就可以访问
		List<GrantedAuthority> authorities = new ArrayList<>();
		authorities.add(new SimpleGrantedAuthority("PERMIT_ALL"));
		authorities.add(new SimpleGrantedAuthority("PERMIT_ALL_2"));
		return authorities;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.userName;
	}

	/**
	 * 账号是否没有过期
	 */
	@Override
	public boolean isAccountNonExpired() {
		return this.accountNonExpired;
	}

	/**
	 * 账号是否没有锁定
	 */
	@Override
	public boolean isAccountNonLocked() {
		return this.accountNonLocked;
	}

	/**
	 * 认证是否没有过期
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		// TODO:修改成根据token判断认证是否已经过期
		// 同时，需要在权限认证的时候对token进行刷新
		return true;
	}

	/**
	 * 账号是否可用
	 */
	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public List<MenuDto> getMenus() {
		return menus;
	}

	public void setMenus(List<MenuDto> menus) {
		this.menus = menus;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getPasswordExpireDays() {
		return passwordExpireDays;
	}

	public void setPasswordExpireDays(Integer passwordExpireDays) {
		this.passwordExpireDays = passwordExpireDays;
	}

	public Date getPasswordExpireTime() {
		return passwordExpireTime;
	}

	public void setPasswordExpireTime(Date passwordExpireTime) {
		this.passwordExpireTime = passwordExpireTime;
	}

	public Integer getAccountExpireDays() {
		return accountExpireDays;
	}

	public void setAccountExpireDays(Integer accountExpireDays) {
		this.accountExpireDays = accountExpireDays;
	}

	public Date getAccountExpireTime() {
		return accountExpireTime;
	}

	public void setAccountExpireTime(Date accountExpireTime) {
		this.accountExpireTime = accountExpireTime;
	}

	public Integer getFailedCount() {
		return failedCount;
	}

	public void setFailedCount(Integer failedCount) {
		this.failedCount = failedCount;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Integer getMaxFailedCount() {
		return maxFailedCount;
	}

	public void setMaxFailedCount(Integer maxFailedCount) {
		this.maxFailedCount = maxFailedCount;
	}

	public boolean isCredentialNonExpired() {
		return credentialNonExpired;
	}

	public void setCredentialNonExpired(boolean credentialNonExpired) {
		this.credentialNonExpired = credentialNonExpired;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<String> getRoles() {
		return roles;
	}

	public void setRoles(List<String> roles) {
		this.roles = roles;
	}
}
